﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Configuration;

namespace NPMonitor.DataModel
{
    [Table("Machines")]
    public class MachineData
    {
        [Key]
        public int MachineId { get; set; }
        public int PoolId { get; set; }
        public string MachineName { get; set; }
        public string MAC { get; set; }
        public string IP { get; set; }
        public string Description { get; set; }
        public DateTime? LastHeartbeat { get; set; }
        public DateTime? RegistDate { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public int SecondsDiff { get; set; }

        [NotMapped]
        public string Connectivity
        {
            get
            {
                if (SecondsDiff < Configuration.ConfigurationSettings.SettingsProvider.HeartbeatFrequency.TotalSeconds + 5)
                {
                    return Connectivities.Connected.ToString();
                }
                else
                {
                    return Connectivities.Unconnected.ToString();
                }
            }
        }

        [NotMapped]
        public int JobCount
        {
            get
            {
                return this.ActiveJobCount(MachineName);
            }
        }

        [NotMapped]
        public string TrafficLight
        {
            get
            {
                if (string.Compare(Connectivity, Connectivities.Connected.ToString(), true) == 0)
                {
                    return TrafficLights.green.ToString();
                }
                else
                {
                    return TrafficLights.red.ToString();
                }

            }
        }
        
        [NotMapped]
        public string PoolName
        {
            get 
            {
                return this.GetPoolNameByPoolId(this.PoolId);
            }
        }

        #region Constructors

        public MachineData() { }

        public MachineData(MachineInfo machineInfo)
        {
            this.MachineName = machineInfo.MachineName;
            this.IP = machineInfo.IPAddress;
            this.MAC = machineInfo.MACAddress;
            this.SecondsDiff = 0;
        }

        #endregion

    }
}
